<?php

namespace app\index\controller;

use think\Db;
use think\Loader;
use think\Request;
use think\exception\HttpResponseException;
use think\Session;
use think\Controller;

class User extends Basic
{
    public function index()
    {
        return $this->fetch();
    }

    public function add()
    {
        return $this->fetch();
    }

    public function select()
    {
        $page = Request::instance()->get('page', null, 'int');
        $limit = Request::instance()->get('limit', null, 'int');
        if ($page) {
            $start = ($page - 1) * $limit;
        } else {
            $start = 0;
        }
        $result = Db::name('admin')
            ->limit($start, $limit)
            ->select();
        $count = Db::name('admin')
            ->count();
        return $result ? json(['code' => 0, 'count' => $count, 'msg' => '搜索成功', 'data' => $result]) : json(['code' => 1, 'msg' => '无该数据', 'data' => $result]);
    }

    public function addUser()
    {
        $post = Request::instance()->post();
        if (isset($post['username']) && isset($post['password']) && isset($post['repass'])&&isset($post['perm'])) {
            if ($post['password'] == $post['repass']) {
                //判断该用户是否存在
                $judge = Db::name('admin')
                    ->where('username', $post['username'])
                    ->find();
                if ($judge) {
                    $this->error('该用户名已存在');
                }

                if($post['perm'] == 0){
                    $perm = $post['perm'];
                    $group = "设计组";
                }elseif ($post['perm'] == 1){
                    $perm = $post['perm'];
                    $group = "商务组";
                }elseif ($post['perm'] == 2){
                    $perm = 0;
                    $group = "工程组";
                }elseif ($post['perm'] == 3){
                    $perm = $post['perm'];
                    $group = "管理员";
                }elseif ($post['perm'] == 4){
                    $perm = 1;
                    $group = "财务组";
                }
                $user = array(
                    'username' => $post['username'],
                    'password' => md5($post['password']),
                    'price' => $perm,
                    'group' => $group,
                    'email' => $post['email'],
                    'name' => $post['name'],
                );
//                $auth = array('admin' => $post['username'], 'price' => $post['perm']);
//                $user["username"]->$post['username'];
//                $user["password"]->md5($post['password']);
                $admin = Db::name('admin')->insert($user);
//                $perm = Db::name('auth')->insert($auth);
                $admin ? $this->success('添加成功') : $this->error('添加失败');

            } else {
                $this->error('两次输入密码不同，请重新输入');
            }
        } else {
            $this->error('请填写完整');
        }
    }

    public function update()
    {
        return $this->fetch();
    }

    public function updatePass()
    {
        $post = Request::instance()->post();
        if (isset($post['password']) && isset($post['newpassword']) && isset($post['repass'])) {
            if ($post['newpassword'] == $post['repass']) {
                $sessionInfo = Session::get("admin");
                $judge = Db::name('admin')
                    ->where('id', $sessionInfo['id'])
                    ->find();
                if (!$post['password'] == $judge['password']) {
                    $this->error('原密码错误');
                }
                $admin = Db::name('admin')
                    ->where("id", $sessionInfo['id'])
                    ->update(['password' => md5($post['newpassword'])]);
                $admin ? $this->success('修改成功') : $this->error('修改失败');

            } else {
                $this->error('两次输入密码不同，请重新输入');
            }
        } else {
            $this->error('请填写完整');
        }
    }

    public function perm($name)
    {
        $group = Db::name('admin')
            ->where("username", "=",$name)
            ->find();
        $this->assign('group',$group);
        return $this->fetch();
    }

    public function upperm()
    {
        $post = Request::instance()->post();
        if($post['perm']==1){
            $gro = "商务组";
        }elseif ($post['perm']==0){
            $gro = "设计组";
        }elseif ($post['perm']==3){
            $gro = "管理员";
        }
        $perm = array(
            'price' => $post['perm'],
            'group' => $gro
        );
        $group = Db::name('admin')
            ->where("username", "=",$post['username'])
            ->update($perm);
        if($group){
            return $this->success('修改成功');
        }else{
            return$this->error('修改失败');
        }
    }

    public function delet()
    {
        $post = Request::instance()->post();
        if (!empty($post['username'])) {
            $admin = Db::name('admin')
                ->where('username', '=', $post['username'])
                ->delete();
            return $admin ? $this->success('删除成功') : $this->error('删除失败');
        } else {
            return $this->error('内部错误');
        }
    }
}
